﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PI_CS_Library;
using PI_CS_Library.PicsNetwork_Information_Model;
using PI_CS_Tools;
using TransportLayer;
using TransportLayer.Manager;

namespace PI_CS_GUI
{
    public partial class AddLinkGUI : Form
    {
        PicsNetworkController _nc;
        GUIDelegate _gui;
        PicsMatrix _src;
        PicsMatrix _dst;

        public AddLinkGUI(PicsNetworkController _nc, GUIDelegate _gui)
        {
            InitializeComponent();

            foreach (PicsMatrix _node in _nc.Network.MatrixResourceList.Values)
            {
                comboBox1.Items.Add(_node.Name);
                comboBox2.Items.Add(_node.Name);
            }

            this._nc = _nc;
            this._gui = _gui;
        }

        public bool CheckLink()
        {
            _src = null;
            _dst = null;

            foreach (PicsMatrix m in _nc.Network.MatrixResourceList.Values)
            {
                if (comboBox1.SelectedItem == m.Name && comboBox2.SelectedItem == m.Name)
                {
                    MessageBox.Show("Two different nodes must be selected.");
                    return false;
                }

                else
                {
                    if (comboBox1.SelectedItem == m.Name)
                        _src = m;
                    if (comboBox2.SelectedItem == m.Name)
                        _dst = m;
                }
            }

            if (_src == null || _dst == null)
            {
                MessageBox.Show("Two different nodes must be selected.");
                return false;
            }

            foreach (PicsLink l in _nc.Network.LinkResourceList.Values)
            {
                if (l.SrcMatrix.Id.Number == _src.Id.Number && l.SinkMatrix.Id.Number == _dst.Id.Number)
                {
                    MessageBox.Show("This link already exists.");
                    return false;
                }
            }
            
            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (CheckLink())
            {
                int _srcPort = Int32.Parse((string)(comboBox3.SelectedItem));
                int _dstPort = Int32.Parse((string)(comboBox4.SelectedItem));
                int _capacity = Int32.Parse((string)(domainUpDown1.SelectedItem));
                float _delay;
                float _errorRate;
                if (textBox1.Visible == false) _delay = 1;
                else _delay = float.Parse(textBox1.Text);
                if (domainUpDown2.Visible == false) _errorRate = (float)0.01;
                else _errorRate = float.Parse(domainUpDown2.SelectedItem.ToString());

                LinkParameters _newParameters = new LinkParameters(_capacity, _delay, _errorRate);

                PicsLink _newLink = _nc.Network.AddNewLink(RandomNumber.Get, _src, _dst, _srcPort, _dstPort, _newParameters);

                //_nc.ManagerEngine.Links.Add(_newLink.Id.Number, _newLink.GetEngineRepresentation(_nc.ManagerEngine));

                _gui(this, new GUIArgs("newLinkOK#" + _newLink.Id.Number));

                this.Close();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            label7.Visible = true;
            textBox1.Visible = true;

            label3.Visible = true;
            domainUpDown2.Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddLinkGUI_Load(object sender, EventArgs e)
        {

        }

        private void AddLinkGUI_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
